<template>
  <div class="app-container">
    <el-row :gutter="10">
      <fold :span="5" :xs="24" :show.sync='sideshow'>
        <el-card shadow="never">
          <org-tree ref="orgTree" modelType="'dept','community'" @nodeClick="handleNodeClick"
                    @firstNodeClick="handleNodeClick"/>
        </el-card>
      </fold>
      <el-col :span="sideshow ? 19 : 24" :xs="24">
        <el-card shadow="never">
          <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
            <el-row :gutter="20">
              <el-col :span="6" :xs="24">
                <el-form-item label="客户姓名" prop="customName">
                  <el-input
                    v-model="queryParams.customName"
                    placeholder="请输入客户姓名"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6" :xs="24">
                <el-form-item label="手机号码" prop="cellPhoneNumber">
                  <el-input
                    v-model="queryParams.cellPhoneNumber"
                    placeholder="请输入手机号码"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6" :xs="24">
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                         v-hasPermi="['waitress:customInfo:add']">新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                         v-hasPermi="['waitress:customInfo:edit']">修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                         @click="handleDelete"
                         v-hasPermi="['waitress:customInfo:remove']">删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="info" plain icon="el-icon-upload2" size="mini"
                         @click="handleImport" v-hasPermi="['waitress:customInfo:import']">导入
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
                         @click="handleExport" v-hasPermi="['waitress:customInfo:export']">导出
              </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="customInfoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="客户头像" align="center" prop="customHeadImg" width="80">
              <template slot-scope="scope">
                <el-avatar shape="square" size="small" :src="scope.row.customHeadImg|splitImgSrcFirst"></el-avatar>
              </template>
            </el-table-column>
            <el-table-column label="小区名称" align="left" prop="communityName" show-overflow-tooltip/>
            <el-table-column label="客户姓名" align="center" prop="customName" width="100">
              <template slot-scope="scope">
                <el-link type="primary" @click="customInfoDetail(scope.row)">{{ scope.row.customName }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="客户类别" align="center" prop="customType" width="100">
              <template slot-scope="scope">
                {{ dict.label.custom_type[scope.row.customType] }}
              </template>
            </el-table-column>
            <el-table-column label="手机号码" align="center" prop="cellPhoneNumber" width="120"/>
            <el-table-column label="身份证号" align="center" prop="idCard" width="160"/>
            <el-table-column label="性别" align="center" prop="sex" width="80">
              <template slot-scope="scope">
                {{ dict.label.sys_user_sex[scope.row.sex] }}
              </template>
            </el-table-column>
            <el-table-column label="出生日期" align="center" prop="birthDate" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
    <CustomInfoForm ref="formRef" @cancel="getList"/>
    <import-data ref="importRefs" @ok="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listCustomInfo, delCustomInfo, exportCustomInfo} from "@/api/waitress/customInfo";
  import CustomInfoForm from "./form.vue"
  import ImportData from "@/components/common/ImportData"

  export default {
    name: "CustomInfo",
    components: {CustomInfoForm,ImportData},
    mixins: [listMixin],
    dicts: ['custom_type', 'sys_user_sex'],
    data() {
      return {
        // 客户管理表格数据
        customInfoList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          communityId: null,
          customName: null,
          customHeadImg: null,
          customType: null,
          cellPhoneNumber: null,
          idCard: null,
          sex: null,
          birthDate: null,
          customNumber: null,
          corporateName: null,
          contactAddress: null,
          linkman: null,
          status: null,
          nodeId:null
        },
        currentNode:{}
      };
    },
    created() {

    },
    methods: {
      /** 查询客户管理列表 */
      getList() {
        this.loading = true;
        listCustomInfo(this.queryParams).then(response => {
          this.customInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.customId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        if(this.currentNode.modelType == 'community'){
          this.$refs.formRef.show({"type" : "ADD", "currentNode" : this.currentNode});
        }else{
          this.msgWarning("请选择小区信息")
        }

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const customId = row.customId || this.ids
        const option = {
          "type": "UPDATE",
          "id": customId
        }
        this.$refs.formRef.show(option);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const customIds = row.customId || this.ids;
        this.$confirm('是否确认删除客户管理编号为"' + customIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delCustomInfo(customIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有客户管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCustomInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      },
      handleNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      customInfoDetail(row){
        this.$router.push({path:'/waitress/customInfo/detail',query: {'idCard' : row.idCard , 'communityId' : row.communityId,'customId':row.customId}})
      },
      handleImport(){
        if(this.currentNode.modelType == 'community'){
          this.$refs.importRefs.show({
            title:`${this.currentNode.label}客户导入`,
            url:'/waitress/customInfo/importData',
            params:{ 'currencyCommunityId' : this.currentNode.id },
            modelType:'customInfo'
          })
        }else{
          this.msgWarning("请选择小区信息")
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep  .el-avatar > img{
    width: 100%;
  }
</style>
